﻿using System;
using SunXNA.CameraManager.BaseCamera;
using Microsoft.Xna.Framework;
using System.Diagnostics;

using Microsoft.Xna.Framework.Input;
using Darwin.Source.Components.World.Entities;

namespace Cameras
{
    class CGameCamera : CBaseCamera
    {
        #region Public Types
        #endregion

        #region Private Member Variables

        private Vector3 m_v3Direction;
        private float m_orientation;
        private float m_fDistance;

        private CCursor m_cursor;

        #endregion

        #region Public Accessors

        public CCursor Cursor
        {
            get
            {
                return m_cursor;
            }

            set
            {
                m_cursor = value;
            }
        }

        #endregion

        #region Public Member Functions

        public CGameCamera()
        {
            m_v3Direction = Vector3.Normalize( new Vector3(0.0f, -1.0f, -1.0f) );
            m_orientation = 0f;
            m_fDistance = 20.0f;

            m_cursor = null;

            m_rFOV = MathHelper.PiOver2;
        }

        public override void Step(GameTime gameTime)
        {
            base.Step(gameTime);

            GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);
            m_orientation += gamePadState.ThumbSticks.Right.X /** (float)gameTime.ElapsedGameTime.TotalSeconds*/;
            m_rFOV -= gamePadState.ThumbSticks.Right.Y * (float)gameTime.ElapsedGameTime.TotalSeconds;

            m_rFOV = MathHelper.Clamp(m_rFOV, MathHelper.Pi / 8.0f, MathHelper.PiOver2);

            Vector3 v3Position = m_cursor.Position - (m_v3Direction * m_fDistance);

            //v3Position.X = m_cursor.Position.X + (float)Math.Sin(MathHelper.ToRadians(m_orientation)) * m_fDistance;
            //v3Position.Z = m_cursor.Position.Z + (float)Math.Cos(MathHelper.ToRadians(m_orientation)) * m_fDistance;

            m_m4CurrentViewMatrix = Matrix.CreateLookAt(v3Position, m_cursor.Position, Vector3.Up);
        }

        #endregion
    }
}
